//@line 38 "/Users/tony/Development/celtx-head/mozilla/celtx/base/resources/content/prefs/network.js"

function networkLoaded () {
  gConnectionsDialog.load();
}


var gConnectionsDialog = {
  load: function () {
    dump("    network pane loaded\n");
    window.addEventListener("beforeaccept", this, false);
  },

  handleEvent: function (event) {
    if (event.type != "beforeaccept") return;
    window.removeEventListener("beforeaccept", this, false);
    this.beforeAccept();
  },

  beforeAccept: function () {
    var proxyTypePref = document.getElementById("network.proxy.type");
    if (proxyTypePref.value == 2) {
      this.doAutoconfigURLFixup();
      return true;
    }

    if (proxyTypePref.value != 1)
      return true;

    var httpProxyURLPref = document.getElementById("network.proxy.http");
    var httpProxyPortPref = document.getElementById("network.proxy.http_port");
    var shareProxiesPref = document.getElementById(
      "network.proxy.share_proxy_settings");
    if (shareProxiesPref.value) {
      var proxyPrefs = ["ssl", "socks"];
      for (var i = 0; i < proxyPrefs.length; ++i) {
        var proxyServerURLPref = document.getElementById(
          "network.proxy." + proxyPrefs[i]);
        var proxyPortPref = document.getElementById(
          "network.proxy." + proxyPrefs[i] + "_port");
        var backupServerURLPref = document.getElementById(
          "network.proxy.backup." + proxyPrefs[i]);
        var backupPortPref = document.getElementById(
          "network.proxy.backup." + proxyPrefs[i] + "_port");

        backupServerURLPref.value = proxyServerURLPref.value;
        backupPortPref.value      = proxyPortPref.value;
        proxyServerURLPref.value  = httpProxyURLPref.value;
        proxyPortPref.value       = httpProxyPortPref.value;
      }
    }

    var noProxiesPref = document.getElementById("network.proxy.no_proxies_on");
    noProxiesPref.value = noProxiesPref.value.replace(/[;]/g,',');

    return true;
  },

  proxyTypeChanged: function () {
    var proxyTypePref = document.getElementById("network.proxy.type");

    // Update http
    var httpProxyURLPref = document.getElementById("network.proxy.http");
    httpProxyURLPref.disabled = proxyTypePref.value != 1;
    var httpProxyPortPref = document.getElementById("network.proxy.http_port");
    httpProxyPortPref.disabled = proxyTypePref.value != 1;

    // Now update the other protocols
    this.updateProtocolPrefs();

    var shareProxiesPref = document.getElementById(
      "network.proxy.share_proxy_settings");
    shareProxiesPref.disabled = proxyTypePref.value != 1;

    var noProxiesPref = document.getElementById(
      "network.proxy.no_proxies_on");
    noProxiesPref.disabled = proxyTypePref.value != 1;

    var autoconfigURLPref = document.getElementById(
      "network.proxy.autoconfig_url");
    autoconfigURLPref.disabled = proxyTypePref.value != 2;

    var disableReloadPref = document.getElementById(
      "pref.advanced.proxies.disable_button.reload");
    disableReloadPref.disabled = proxyTypePref.value != 2;
  },

  readProxyType: function () {
    this.proxyTypeChanged();
    return undefined;
  },

  updateProtocolPrefs: function () {
    var proxyTypePref = document.getElementById("network.proxy.type");
    var shareProxiesPref = document.getElementById(
      "network.proxy.share_proxy_settings");
    var proxyPrefs = ["ssl", "socks"];
    for (var i = 0; i < proxyPrefs.length; ++i) {
      var proxyServerURLPref = document.getElementById(
        "network.proxy." + proxyPrefs[i]);
      var proxyPortPref = document.getElementById(
        "network.proxy." + proxyPrefs[i] + "_port");
      
      // Restore previous per-proxy custom settings, if present. 
      if (!shareProxiesPref.value) {
        var backupServerURLPref = document.getElementById(
          "network.proxy.backup." + proxyPrefs[i]);
        var backupPortPref = document.getElementById(
          "network.proxy.backup." + proxyPrefs[i] + "_port");
        if (backupServerURLPref.hasUserValue) {
          proxyServerURLPref.value = backupServerURLPref.value;
          backupServerURLPref.reset();
        }
        if (backupPortPref.hasUserValue) {
          proxyPortPref.value = backupPortPref.value;
          backupPortPref.reset();
        }
      }

      proxyServerURLPref.updateElements();
      proxyPortPref.updateElements();
      proxyServerURLPref.disabled = proxyTypePref.value != 1
        || shareProxiesPref.value;
      proxyPortPref.disabled = proxyServerURLPref.disabled;
    }
    var socksVersionPref = document.getElementById(
      "network.proxy.socks_version");
    socksVersionPref.disabled = proxyTypePref.value != 1
      || shareProxiesPref.value;

    return undefined;
  },

  readProxyProtocolPref: function (aProtocol, aIsPort) {
    var shareProxiesPref = document.getElementById(
      "network.proxy.share_proxy_settings");
    if (shareProxiesPref.value) {
      var pref = document.getElementById(
        "network.proxy.http" + (aIsPort ? "_port" : ""));    
      return pref.value;
    }

    var backupPref = document.getElementById(
      "network.proxy.backup." + aProtocol + (aIsPort ? "_port" : ""));
    return backupPref.hasUserValue ? backupPref.value : undefined;
  },

  reloadPAC: function () {
    var autoURL = document.getElementById("networkProxyAutoconfigURL");
    var pps = Components.classes[
      "@mozilla.org/network/protocol-proxy-service;1"]
      .getService(Components.interfaces.nsPIProtocolProxyService);
    pps.configureFromPAC(autoURL.value);
  },

  doAutoconfigURLFixup: function () {
    var autoURL = document.getElementById("networkProxyAutoconfigURL");
    var autoURLPref = document.getElementById("network.proxy.autoconfig_url");
    var URIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
                             .getService(Components.interfaces.nsIURIFixup);
    try {
      autoURLPref.value = autoURL.value = URIFixup.createFixupURI(
        autoURL.value, 0).spec;
    } catch(ex) {}
  },

  readHTTPProxyServer: function () {
    var shareProxiesPref = document.getElementById(
      "network.proxy.share_proxy_settings");
    if (shareProxiesPref.value)
      this.updateProtocolPrefs();
    return undefined;
  },

  readHTTPProxyPort: function () {
    var shareProxiesPref = document.getElementById(
      "network.proxy.share_proxy_settings");
    if (shareProxiesPref.value)
      this.updateProtocolPrefs();
    return undefined;
  }
};
